package model

import (
	"choujiang/common/db"
	"fmt"
)

type Orders struct {
	Id          int64   `xorm:"INT(11) not null pk autoincr"`
	OrderSn     string  `xorm:"VARCHAR(255)"`
	OrderType   int64   `xorm:"INT(3) not null default 1 comment(1商品/2游戏)"`
	GoodsId     int64   `xorm:"INT(11) not null default 0"`
	GoodsNum    int64   `xorm:"INT(11) not null default 0"`
	UserId      int64   `xorm:"INT(11) not null default 0"`
	Cost        float64 `xorm:"DECIMAL(10) not null default 0.00"`
	PayType     int64   `xorm:"INT(3) not null default 1 comment(1金币支付)"`
	OrderStatus int64   `xorm:"INT(3) not null default 1 comment(1已支付未发货/2已支付已发货)"`
	CreatedAt   int64   `xorm:"INT(11) not null default 0"`
	UpdatedAt   int64   `xorm:"INT(11) not null default 0"`
	PayAt       int64   `xorm:"INT(11) not null default 0"`
	SendAt      int64   `xorm:"INT(11) not null default 0"`
	Receiver    string  `xorm:"VARCHAR(50)"`
	Phone       string  `xorm:"VARCHAR(20)"`
	Address     string  `xorm:"VARCHAR(255)"`
	Channel     string  `xorm:"VARCHAR(50)"`
	GoodsInfo   *Goods  `xorm:"-"`
}

const (
	POINT_PAY         = 1 //金币支付
	ORDER_STATUS_PAY  = 1 //已付款未发货
	ORDER_STATUS_SEND = 2 //已付款已发货
)

func (o *Orders) Insert() (*Orders, error) {
	_, err := db.MysqlEngine.InsertOne(o)
	return o, err
}

func (o *Orders) Update() (*Orders, error) {
	_, err := db.MysqlEngine.ID(o.Id).Update(o)
	return o, err
}

func GetOrderByOrderSn(orderSn string) *Orders {
	order := new(Orders)
	has, err := db.MysqlEngine.Where("order_sn=?", orderSn).Get(order)
	fmt.Printf("GetOrderByOrderSn err %+v\n", err)
	if !has || err != nil {
		return nil
	}
	return order
}
